class ContentController < ApplicationController
  #before_filter :auth
  skip_before_filter :verify_authenticity_token

	def index 
		redirect_to :action => :news
	end


	def news
		@news = New.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")
	end

	def edit_news
		@new = New.find_by_id(params[:id])
	end

  def destroy_news
    @news = New.find(params[:id])
    @news.destroy
		redirect_to :action => :news
  end 

	def up_news
    @new = New.find(params[:id])
    @new.content = params[:message]
    if @new.update_attributes(params[:new])
			@new.clear_up_categories
			@new.add_categories(params[:categories])
      @new.add_images(params[:files])
      flash[:notice]="修改成功！"
    else
      flash[:notice]="修改失败！"
    end
		redirect_to :action => :news
	end

	def create_news
		@news = New.new(params[:new])
		@news.content = params[:message]
		if @news.save
			@news.add_categories(params[:categories])
      @news.add_images(params[:files])
			flash[:notice] = "添加成功"
		else
			flash[:notice] = "修改成功"
		end
		redirect_to :action => :news
	end

	def researchs
		@researchs = Research.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")
	end
	
	def add_research
	  @research = Research.new
  end

	def edit_research
		@research = Research.find_by_id(params[:id])
	end

	def create_research
		@research = Research.new(params[:research])
		@research.content = params[:message]
		if @research.save
			flash[:notice] = "添加成功"
		else
			flash[:notice] = "添加失败"
		end
		redirect_to :action => :researchs
	end

	def up_research
		@research = Research.find_by_id(params[:id])
		@research.content = params[:message]
		if @research.update_attributes(params[:research])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
		redirect_to :action => :researchs
	end

  def destroy_research
		research = Research.find(params[:id])
    research.destroy
		redirect_to :action => :researchs
  end 


	def faquestions
		@faquestions = Faquestion.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")
	end
	
	def add_faquestion
	  @faquestion = Faquestion.new
  end

	def edit_faquestion
		@faquestion = Faquestion.find_by_id(params[:id])
	end

	def create_faquestion
		@faquestion = Faquestion.new(params[:faquestion])
		@faquestion.content = params[:message]
		if @faquestion.save
		  @faquestion.add_categories(params[:categories])
			flash[:notice] = "添加成功"
		else
			flash[:notice] = "添加失败"
		end
		redirect_to :action => :faquestions
	end

	def up_faquestion
		@faquestion = Faquestion.find_by_id(params[:id])
		@faquestion.content = params[:message]
		if @faquestion.update_attributes(params[:faquestion])
		  @faquestion.clear_up_categories
		  @faquestion.add_categories(params[:categories])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
		redirect_to :action => :faquestions
	end

  def destroy_faquestion
		faquestion = Faquestion.find(params[:id])
    faquestion.destroy
		redirect_to :action => :faquestions
  end

	def downloads
		@downloads = Download.paginate(:page => params[:page], :per_page => 10, :order => "created_at DESC")
	end
	
	def add_download
	  @download = Download.new
  end

	def edit_download
		@download = Download.find_by_id(params[:id])
	end

	def create_download
		@download = Download.new(params[:download])
		@download.content = params[:message]
		if @download.save
			flash[:notice] = "添加成功"
		else
			flash[:notice] = "添加失败"
		end
		redirect_to :action => :downloads
	end

	def up_download
		@download = Download.find_by_id(params[:id])
		@download.content = params[:message]
		if @download.update_attributes(params[:download])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
		redirect_to :action => :downloads
	end

  def destroy_download
		download = Download.find(params[:id])
    download.destroy
		redirect_to :action => :downloads
  end

	def files
		@download = Download.find(params[:id])
	end

	def add_file
		@download = Download.find(params[:id])
	end

  def create_file
		download  = Download.find(params[:id])
    @downfile = Downfile.new(:uploaded_data => params[:downfile][:uploaded_data])
		@downfile.download_id = download.id
    @downfile.name 		 		= params[:downfile][:uploaded_data].original_filename
    @downfile.filename 		= params[:id].to_s + Date.new.strftime("%Y%m%d%H%M%S")+@downfile.filename.to_s
     if @downfile.save
       flash[:notice]="添加成功！"
     else
       flash[:notice]="添加失败,请检查文件格式"
     end
    redirect_to(:action => :files, :id => download.id )
  end

	def destroy_file
		file = Downfile.find(params[:id])
		download = file.download
		Downfile.delete_all(:id => params[:id])
		redirect_to :action => :files, :id => download.id
	end

	def pages
		@pages = Page.paginate(:page => params[:page], :per_page => 10, :order => "position DESC")
	end
	
	def add_page
	  @page = Page.new
  end

	def create_page 
		page = Page.new(params[:page])
		page.content = params[:message]
		if page.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :pages
	end

	def edit_page
		@page = Page.find(params[:id])
	end

	def up_page
		page = Page.find(params[:id])
		page.content = params[:message]
		if page.update_attributes(params[:page])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :pages
	end

	def destroy_page
		page = Page.find(params[:id])
		page.destroy if page
		redirect_to :action => :pages
	end
	
	def categories
		@categories = Category.paginate(:page => params[:page], :per_page => 10, :order => "created_at DESC")
	end


	def create_category 
		category = Category.new(params[:category])
		if category.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :categories
	end

	def edit_category
		@category = Category.find(params[:id])
	end

	def up_category
		category = Category.find(params[:id])
		if category.update_attributes(params[:category])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :categories
	end

	def destroy_category
		category = Category.find(params[:id])
		category.destroy if category
		redirect_to :action => :categories
	end

	def resources
		@resources = Resource.paginate(:page => params[:page], :per_page => 10)
	end
	
	def add_resource
	  @resource = Resource.new
  end

	def create_resource 
		resource = Resource.new(params[:resource])
		resource.content = params[:message]
		if resource.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :resources
	end

	def edit_resource
		@resource = Resource.find(params[:id])
	end

	def up_resource
		resource = Resource.find(params[:id])
		resource.content = params[:message]
		if resource.update_attributes(params[:resource])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :resources
	end

	def destroy_resource
		resource = Resource.find(params[:id])
		resource.destroy if resource
		redirect_to :action => :resources
	end

  def upload_image
    image = ResourceImage.new(:image => params[:file], :belong_type => params[:type])
    image.name = image.image.original_filename
    image.description = params[:desc]
    if image.save
      render :text => render_to_string(:partial => "file_field", :locals => {:file => image})
    else
      render :text => "上传失败"
    end
  end


	def faq_categories
		@faq_categories = FaqCategory.paginate(:page => params[:page], :per_page => 10)
	end

	def create_faq_category 
		faq_category = FaqCategory.new(params[:faq_category])
		if faq_category.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :faq_categories
	end

	def edit_faq_category
		@faq_category = FaqCategory.find(params[:id])
	end

	def up_faq_category
		faq_category = FaqCategory.find(params[:id])
		if faq_category.update_attributes(params[:faq_category])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :faq_categories
	end

	def destroy_faq_category
		faq_category = FaqCategory.find(params[:id])
		faq_category.destroy if faq_category
		redirect_to :action => :faq_categories
	end

	def solutions
		@solutions = Solution.paginate(:page => params[:page], :per_page => 10, :order => "created_at DESC")
	end
	
	def add_solution
	  @solution = Solution.new
  end

	def create_solution 
		solution = Solution.new(params[:solution])
		solution.content = params[:message]
		if solution.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :solutions
	end

	def edit_solution
		@solution = Solution.find(params[:id])
	end

	def up_solution
		solution = Solution.find(params[:id])
		solution.content = params[:message]
		if solution.update_attributes(params[:solution])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :solutions
	end

	def destroy_solution
		solution = Solution.find(params[:id])
		solution.destroy if solution
		redirect_to :action => :solutions
	end

	def research_categories
		@research_categories = ResearchCategory.paginate(:page => params[:page], :per_page => 10)
	end
	
	def add_research_category
	  @research_category = ResearchCategory.new
  end

	def create_research_category 
		research_category = ResearchCategory.new(params[:research_category])
		if research_category.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :research_categories
	end

	def edit_research_category
		@research_category = ResearchCategory.find(params[:id])
	end

	def up_research_category
		research_category = ResearchCategory.find(params[:id])
		if research_category.update_attributes(params[:research_category])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :research_categories
	end

	def destroy_research_category
		research_category = ResearchCategory.find(params[:id])
		research_category.destroy if research_category
		redirect_to :action => :research_categories
	end

  def upload_file_to_article
    file = UploadFile.new(params[:file])
    file_info = file.save
    render :text => file_info[:url]
  end

	def fronts
		@fronts = Front.paginate(:page => params[:page], :per_page => 10)
	end
	
	def add_front
	  @front = Front.new
  end

	def create_front 
		front = Front.new(params[:front])
		if front.save
      flash[:notice]="添加成功！"
		else
      flash[:notice]="添加失败！"
		end
	 	redirect_to :action => :fronts
	end

	def edit_front
		@front = Front.find(params[:id])
	end

	def up_front
		front = Front.find(params[:id])
		if front.update_attributes(params[:front])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
 	  redirect_to :action => :fronts
	end

	def destroy_front
		front = Front.find(params[:id])
		front.destroy if front
		redirect_to :action => :fronts
	end

	def members
		@members = Member.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")
	end
	
	def add_member
	  @member = Member.new
  end

	def edit_member
		@member = Member.find_by_id(params[:id])
	end

	def create_member
		@member = Member.new(params[:member])
		@member.content = params[:message]
		if @member.save
			flash[:notice] = "添加成功"
		else
			flash[:notice] = "添加失败"
		end
		redirect_to :action => :members
	end

	def up_member
		@member = Member.find_by_id(params[:id])
		@member.content = params[:message]
		if @member.update_attributes(params[:member])
			flash[:notice] = "更新成功"
		else
			flash[:notice] = "更新失败"
		end
		redirect_to :action => :members
	end

  def destroy_member
		member = Member.find(params[:id])
    member.destroy
		redirect_to :action => :members
  end 





end
